package com.firesrc.web;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;
import jodd.db.pool.CoreConnectionPool;

import com.firesrc.bean.Record;
import com.firesrc.db.DBManager;

/**
 * Servlet implementation class QueryMailByKeyword
 */
public class QueryMailByKeyword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QueryMailByKeyword() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("GBK");
		response.setContentType("text/html; charset=gb2312");
		request.setCharacterEncoding("UTF-8");
		String tmpKeyword = request.getParameter("keyword");
		//		tmpKeyword = new String(request.getParameter("keyword").getBytes("ISO8859_1"));
		//		tmpKeyword = new String(request.getParameter("keyword").getBytes("UTF-8"), "GBK");
		//		String string = new String(tmpKeyword.getBytes("iso-8859-1"), ("gb2312"));
		System.out.println("你查询的关键词是:" + tmpKeyword);
		CoreConnectionPool cp = DBManager.getPublicConnectionPool();
		new DbThreadSession(cp);
		DbOomQuery q = new DbOomQuery("select * from record where keyword='" + tmpKeyword + "'");
		List<Record> list = q.list(Record.class);

		Set<Record> set = new HashSet<Record>();
		for (Record record : list) {
			set.add(record);
		}
		StringBuffer buffer = new StringBuffer();
		StringBuffer tableBuffer = new StringBuffer();
		tableBuffer.append("<table border='2'>");
		tableBuffer.append("<tr><td>关键字</td><td>邮箱</td><td>网址</td></tr>");
		request.setAttribute("keywordName", tmpKeyword);
		request.setAttribute("set", set);
		//		for (Record record : set) {
		//			buffer.append(record.getMail() + ";");
		//			tableBuffer.append("<tr><td>" + record.getKeyword() + "</td><td>" + record.getMail() + "</td><td>"
		//					+ record.getUrl() + "</td></tr>");
		//		}
		//		tableBuffer.append("</table>");
		//		String textArea = "<textarea rows='10' cols='100' >" + buffer + "</textarea>";
		//		PrintWriter writer = response.getWriter();
		//		writer.print("<br/>你查的关键字是:<" + tmpKeyword + "> 一共是" + set.size() + "条,具体如下:</br>" + textArea + "</br>");
		//		writer.println(tableBuffer);
		request.getRequestDispatcher("detailMailListByKeyword.jsp").forward(request, response);
	}

}
